package com.loves.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loves.admin.model.ProductCategory;
import com.loves.admin.service.ProductCategoryService;
import com.loves.admin.util.result.ResultApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/product/category")
@Api(value = "商品分类接口")
public class ProductCategoryController {

    @Resource
    private ProductCategoryService productCategoryService;

    @GetMapping("/selectByParent")
    @ApiOperation(value = "获取第一级商品分类列表")
    public ResultApi selectByParent(HttpServletRequest request){
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        Page<ProductCategory> page = new Page<>(pageNo,pageSize);
        return ResultApi.success(productCategoryService.selectByParent(page));
    }

    @GetMapping("/selectByItem")
    @ApiOperation(value = "查看下级",notes = "根据父ID查询商品分类")
    public ResultApi selectByItem(HttpServletRequest request,@NotEmpty(message = "父类不能为空！") @RequestParam String id){
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        Page<ProductCategory> page = new Page<>(pageNo,pageSize);
        return ResultApi.success(productCategoryService.selectByItem(page,id));
    }

    @PostMapping("/create")
    @ApiOperation(value = "添加商品分类")
    public ResultApi create(@RequestBody @NotNull(message = "数据不能为空！") ProductCategory productCategory){
        if(productCategoryService.create(productCategory) > 0){
            return ResultApi.success("新增成功！");
        }else {
            return ResultApi.error("新增失败，请重新填写！");
        }
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改商品分类")
    public ResultApi update(@RequestBody @NotNull(message = "数据不能为空") ProductCategory productCategory){
        if(productCategoryService.update(productCategory) > 0){
            return ResultApi.success("修改成功！");
        }else {
            return ResultApi.error("修改失败，请重新修改！");
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除商品分类")

    public ResultApi delete(@RequestParam @NotEmpty(message = "ID不能为空！") String id){
        if(productCategoryService.delete(id) > 0){
            return ResultApi.success("删除成功！");
        }else {
            return ResultApi.error("删除失败，请重新填写！");
        }
    }


}
